package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.UsersDao;
import com.model.Users;

/**
 * Servlet implementation class FormRequestServlet
 */
@WebServlet("/user/manager")
public class UserManagerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String ctx;
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String func = (String) request.getParameter("func");
		ctx=request.getContextPath();

		if ("login".equals(func))
			login(request, response);
		else if("logout".equals(func))
			logout(request, response);
		else if("add".equals(func))
			add(request, response);
		else if ("addUser".equals(func))
			delete(request, response);
		else if("update".equals(func))
			add(request, response);
		else if ("delete".equals(func))
			delete(request, response);
		else if ("edit".equals(func))
			delete(request, response);
		else show(request, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
			request.getSession().removeAttribute("loginUser");
			request.getSession().removeAttribute("msg");
			request.getRequestDispatcher("/message/manager").forward(request, response);
	}
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("username");
		String psw = request.getParameter("password");
		Users u = new UsersDao().get(name, psw);
		if (u == null)
			response.sendRedirect(ctx+"/user/login.jsp");
		else {
			request.getSession().setAttribute("loginUser", u);
			request.getRequestDispatcher("/message/manager").forward(request, response);
//			response.sendRedirect(ctx+"/message/manager");
		}

	}
	
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String name=request.getParameter("username");
		String psw=request.getParameter("password");
		Users u=new Users();
		u.setUsername(name);
		u.setPassword(psw);
		u.setRoleId(1);
		new UsersDao().insert(u);
		show(request, response);
	}
	private void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				request.setCharacterEncoding("utf-8");
				response.setContentType("text/html;charset=utf-8");
				String name=request.getParameter("username");
				String psw=request.getParameter("password");
				new UsersDao().insert(name, psw);
				Users u = new UsersDao().get(name, psw);
				request.getSession().setAttribute("loginUser", u);
				request.getRequestDispatcher("/message/manager").forward(request, response);
		}
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String id=request.getParameter("id");
		String name=request.getParameter("username");
		String psw=request.getParameter("password");
		Users u=new Users();
		u.setUsername(name);
		u.setPassword(psw);
		u.setId(Integer.parseInt(id));
		new UsersDao().update(u);
		request.getSession().setAttribute("loginUser", u);
		request.getRequestDispatcher("/message/manager").forward(request, response);
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String id=request.getParameter("id");		
		String name=request.getParameter("username");
		String psw=request.getParameter("password");
		String roleid=request.getParameter("roleId");
	
		Users u=new Users();
		u.setId(Integer.parseInt(id));
		u.setUsername(name);
		u.setPassword(psw);
		u.setRoleId(Integer.parseInt(roleid));
		new UsersDao().update(u);
		show(request, response);
	}
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		new UsersDao().delete(Integer.parseInt(id));
		show(request, response);
	}
	
	private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Users> list = new UsersDao().getAll();
		request.setAttribute("list", list);
		request.getRequestDispatcher("/user/showUsers.jsp").forward(request, response);
	}
}
